<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>拖拽上传图片</title>
    <style>
        body{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        .conatiner{
           position: relative;
           left: 0;
           top: 0;
           display: flex;
           flex-direction: column;
           justify-content: center;
           align-items: center;
           width: 100vw;
           min-height: 100vh;
           background-color: #f2f2f2;
        }
        #uploadArea{
            position: relative;
            width: 300px;
            height: 300px;
            line-height: 300px;
            outline: 1px dashed aqua;
            font-size: 14px;
            display: flex;
            align-items: center;
            justify-content: center;
            
        }
        #uploadArea>span{
            position: absolute;
            left: 50%;
            transform: translateX(-50%);
            z-index: 0;
        }
        #uploadArea > input{
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            opacity: 0;
            cursor: pointer;
        }
        #uploadArea>img{
            position: relative;
            z-index: 1;
            max-width: 100%;
            max-height: 100%;
            margin: auto;
          
        }
        .conatiner>input{
            margin-top: 30px;
        }

    </style>
</head>

<body>
    <div class="conatiner">
        <div id="uploadArea">
            <span>可将图片拖到此处上传</span>
            
            <img alt="">
            <input type="file" name="" id="">
        </div>
        <input type="button" value="上传">
    </div>
    
    <script>
        const uploadAreaDom = document.getElementById("uploadArea")

        const imgDom = document.querySelector('img')

        const spanDom = document.querySelector("span")

        const fileInput = document.querySelector("input")

        let file

        let flag = false

        // 文件经过元素时
        uploadAreaDom.addEventListener("dragover",function (e) {
            e.dataTransfer.dropEffect = 'move'

            if(!flag){
                spanDom.innerHTML = "松手即可上传"
            }
            
            e.stopPropagation()
            // 阻止浏览器打开文件操作
            e.preventDefault()
        })

        // 鼠标松开
        uploadAreaDom.addEventListener("drop",function (e) {
            // 单图上传
            file = e.dataTransfer.files[0]

            // 预览模式
            imgDom.setAttribute('src',fileTemPath(e.dataTransfer.files))

            flag = true
            e.stopPropagation()
            // 阻止浏览器打开文件操作
            e.preventDefault()
        })


        // 照片移出时
        document.ondragover = function (e) {
            if(!flag){
                spanDom.innerHTML = "可将图片拖到此处上传"
            }
        }

        // 点击文件上传
        fileInput.addEventListener("change",function (e) {
            console.log(e.target.files);
            file = e.target.files[0]
            imgDom.setAttribute('src',fileTemPath(e.target.files))
        })
        

        // 返回文件临时路径方法
        function fileTemPath(files) {
            if(files.length == 1){
                return URL.createObjectURL(files[0])
            }else{
                
            }
        }
        
    </script>

    <body>

</html>